<?php

namespace app\modules\admin\controllers;

use app\common\controllers\AccessController;
use app\modules\admin\forms\goods\GoodsForm;
use app\modules\admin\services\GoodsService;
use yii\base\UserException;

class GoodsController extends AccessController{

    //搜索
    public function actionSearch(){
        $merchant_id = \Yii::$app->getRequest()->get('merchant_id');
        $goods_name = \Yii::$app->getRequest()->get('goods_name');
        $cat_id = \Yii::$app->getRequest()->get('cat_id');
        $is_sale = \Yii::$app->getRequest()->get('is_sale');
        $pagesize = \Yii::$app->getRequest()->get('pagesize');

        return [
            'list'=>GoodsService::search($merchant_id,$goods_name,$cat_id,$is_sale,$page,$pagesize),
            'page'=>$page->Out(),
        ];
    }

    //添加
    public function actionAdd(){
        $form = new GoodsForm();
        $form->setScenario("create");
        if($form->load(\Yii::$app->getRequest()->post(),'') && GoodsService::create($form)){
            return "";
        }else{
            throw new UserException(GoodsService::getError());
        }
    }

    //编辑
    public function actionEdit(){
        $form = new GoodsForm();
        $form->setScenario("update");
        if($form->load(\Yii::$app->getRequest()->post(),'') && GoodsService::update($form)){
            return "";
        }else{
            throw new UserException(GoodsService::getError());
        }
    }

    //获取信息
    public function actionGetInfoById($merchant_id,$id){
        return GoodsService::getInfoById($merchant_id,$id);
    }

    //删除
    public function actionDelete($merchant_id,$id){
        GoodsService::delete($merchant_id,$id);
        return "";
    }
}